/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 *
 * @version 1.0
 */
package shop.controller.front.retroactive;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.account.bean.Account;
import shop.services.front.offlineAccount.OfflineAccountService;
import shop.services.front.offlineAccount.bean.OfflineAccount;
import shop.services.front.organize.OrganizeService;
import shop.services.front.organize.bean.Organize;
import shop.services.front.retroactive.RetroactiveService;
import shop.services.front.retroactive.bean.Retroactive;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名称：RetroactiveController
 * @创建人：Ltz
 * @创建时间：2017-08-12 上午10:39:07
 * @版本号：1.0
 * @描述：
 */
@Controller
@RequestMapping("/front/retroactive")
public class RetroactiveController extends FrontBaseController<Retroactive> {
    @Resource
    private RetroactiveService retroactiveService;
    @Resource
    private OrganizeService organizeService;
    @Resource
    private OfflineAccountService offlineAccountService;

    @Override
    public Services<Retroactive> getService() {
        return retroactiveService;
    }

    /**
     * 获取待审批列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/initList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult initList(HttpServletRequest request) {
        Organize organize = (Organize) request.getSession().getAttribute("organizeInfo");
        return new JSONResult(organizeService.getRetroactiveList(organize), true);
    }

    /**
     * 获取审批记录
     * @param request
     * @return
     */
    @RequestMapping(value = "/approvalList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult approvalList(HttpServletRequest request) {
        Organize organize = (Organize) request.getSession().getAttribute("organizeInfo");
        return new JSONResult(organizeService.getApprovalList(organize), true);
    }

    /**
     * 审批操作
     * @param retroactive 要审批的记录
     * @param request     用于获取当前登录用户
     * @return 请求结果
     * @throws Exception
     */
    @RequestMapping(value = "/approval", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult approval(Retroactive retroactive, HttpServletRequest request) throws Exception {
        Organize organize = (Organize) request.getSession().getAttribute("organizeInfo");
        retroactive.setAuditor(organize.getPrincipal());
        super.updateJson(request, retroactive);
        return JSONResult.success("审批成功");
    }

    /**
     * 获取申请记录详情
     * @param retroactive 要获取的记录
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult detail(Retroactive retroactive) {
        return new JSONResult(getService().selectOne(retroactive), true);
    }

    /**
     * 打卡记录
     * @param offlineAccount 要查询记录的活动
     * @return
     */
    @RequestMapping(value = "/signRecords", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult signRecords(OfflineAccount offlineAccount) {
        List<OfflineAccount> offlineAccountList = offlineAccountService.getRecords(offlineAccount);
        List<OfflineAccount> signInList = new ArrayList<>();
        List<OfflineAccount> signOutList = new ArrayList<>();
        for (OfflineAccount anOfflineAccount : offlineAccountList) {
            if (!StringUtils.isBlank(anOfflineAccount.getSignInTime()))
                signInList.add(anOfflineAccount);
            if (!StringUtils.isBlank(anOfflineAccount.getSignOutTime()))
                signOutList.add(anOfflineAccount);
        }
        Map data = new HashMap<String, Object>();
        data.put("signInList", signInList);
        data.put("signOutList", signOutList);
        return new JSONResult(data, true);
    }

    /**
     * 申请记录
     * @param retroactive 要查询审核记录的活动
     * @param request 用于获取当前登录用户
     * @return
     */
    @RequestMapping(value = "/approvalRecord", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult approvalRecord(Retroactive retroactive, HttpServletRequest request) {
        Account account = (Account) request.getSession().getAttribute("accountInfo");
        retroactive.setUserId(account.getId());
        List<Retroactive> retroactives = retroactiveService.getApprovalRecord(retroactive);
        Map data = new HashMap<String, Object>();
        if (retroactives.size() != 0) {
            for (Retroactive retroactive1 : retroactives) {
                if (retroactive1.getSignType().equals("in"))
                    data.put("inRecord",retroactive1);
                if (retroactive1.getSignType().equals("out"))
                    data.put("outRecord",retroactive1);
            }
        }
        return new JSONResult(data,true);
    }
}
